import 'entity_factory.dart';

abstract class AbstractEntity {
  bool success;
  int code;
  String message;
  String timestamp;

  AbstractEntity({this.success, this.code, this.message, this.timestamp});
}

class BaseEntity<T> extends AbstractEntity {
  T data;
  BaseEntity({success, code, msg, timestamp, this.data})
      : super(success: success, code: code, message: msg, timestamp: timestamp);

  factory BaseEntity.fromJson(json) => BaseEntity(
      success: json['success'],
      code: json['code'],
      msg: json['message'],
      timestamp: json['timestamp'],
      data: EntityFactory.generateOBJ<T>(json['data']));
}

class BaseEntityList<T> extends AbstractEntity {
  List<T> data;
  BaseEntityList({success, code, msg, timestamp, this.data})
      : super(success: success, code: code, message: msg, timestamp: timestamp);

  factory BaseEntityList.fromJson(json) {
    var dataList = List<T>();
    if (json['data'] != null) {
      (json['data'] as List).forEach((v) {
        dataList.add(EntityFactory.generateOBJ<T>(v));
      });
    }
    return BaseEntityList(
      success: json['success'],
      code: json['code'],
      msg: json['message'],
      timestamp: json['timestamp'],
      data: dataList,
    );
  }
}
